package com.pdg.mcplugin.common;

import java.util.Iterator;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;

public class PlayerUtilities {
	public static int getPlayerItemCount(Player thePlayer,MaterialData theMaterialData) {
		int theResult = 0;
		Iterator<ItemStack> iterator = thePlayer.getInventory().iterator();
		while(iterator.hasNext()){
			ItemStack theItemStack = iterator.next();
			if(theItemStack==null) continue;
			if(theItemStack.getData().getItemType()==theMaterialData.getItemType() && theItemStack.getData().getData() == theMaterialData.getData()){
				theResult += theItemStack.getAmount();
			}
		}
		return theResult;
	}

	@SuppressWarnings("deprecation")
	public static void removePlayerItemCount(Player thePlayer, MaterialData theMaterialData,
			int theCount) {
		Iterator<ItemStack> iterator = thePlayer.getInventory().iterator();
		while(iterator.hasNext() && theCount>0){
			ItemStack theItemStack = iterator.next();
			if(theItemStack==null) continue;
			if(theItemStack.getData().getItemType()==theMaterialData.getItemType() && theItemStack.getData().getData() == theMaterialData.getData()){
				if(theItemStack.getAmount()<=theCount){
					theCount -= theItemStack.getAmount();
					thePlayer.getInventory().remove(theItemStack);
				}else{
					theItemStack.setAmount(theItemStack.getAmount()-theCount);
					theCount = 0;
				}
			}
		}
		thePlayer.updateInventory();
	}

}
